package sh.wnxy.wn38.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import sh.wnxy.wn38.entity.Role;
import sh.wnxy.wn38.entity.User;
import sh.wnxy.wn38.entity.Userrole;
import sh.wnxy.wn38.service.*;
import sh.wnxy.wn38.service.vo.UserQuery;
import sh.wnxy.wn38.service.vo.usc;
import sh.wnxy.wn38.utils.Result;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 * 用户，景点，订单，评论
 *
 * @author wn38
 * @since 2021-07-14
 */
@Slf4j
@CrossOrigin
@ApiModel("管理员后台")
@RestController
@RequestMapping("/admin")
public class AdminController {
    private IUserroleService iUserroleService;
    private IUserService iUserService;

    private ISpotService iSpotService;

    private IImgService iImgService;
    private IRoleService ir;
    @Autowired
    public void setIr(IRoleService ir) {
        this.ir = ir;
    }
    @Autowired
    public void setiUserroleService(IUserroleService iUserroleService) {
        this.iUserroleService = iUserroleService;
    }
    @Autowired
    public void setiUserService(IUserService iUserService) {
        this.iUserService = iUserService;
    }

    @Autowired
    public void setiSpotService(ISpotService iSpotService) {
        this.iSpotService = iSpotService;
    }

    @Autowired
    public void setiImgService(IImgService iImgService) {
        this.iImgService = iImgService;
    }

    @ApiOperation("管理员登录")
    @PostMapping("/login")
    public Result login(@RequestBody usc user) {
        log.warn(user.toString());
        QueryWrapper<User> Wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(user.getUserPwd())){
            Wrapper.eq("user_pwd",user.getUserPwd());
        }
        if(!StringUtils.isEmpty(user.getUserName())){
            Wrapper.eq("user_name",user.getUserName());
        }
        User one = iUserService.getOne(Wrapper);
        System.out.println(one);
        if(one==null){
            return Result.error().msg("用户名或密码错误");
        }
        QueryWrapper<Userrole> WR = new QueryWrapper<>();
        WR.eq("ur_userid",one.getUserId());
        Userrole one1 = iUserroleService.getOne(WR);
        System.out.println(one1);
        if(one1==null){
            return Result.error().msg("您还未注册管理系统");
        }
        return Result.ok().data("token", one.getUserId());
    }
    @GetMapping("/info/{token}")
    public Result getUserInfo(@PathVariable("token")String token) {
        User byId = iUserService.getById(token);
        QueryWrapper<Userrole> WR = new QueryWrapper<>();
        WR.eq("ur_userid",byId.getUserId());
        Userrole one1 = iUserroleService.getOne(WR);
        QueryWrapper<Role> ac = new QueryWrapper<>();
        ac.eq("role_id",one1.getUrRoleid());
        Role one = ir.getOne(ac);
        return Result.ok().data("roles", one.getRoleName()).data("name", byId.getUserName()).data("1",1).data("avatar", "https://k15-hxh.oss-cn-shanghai.aliyuncs.com/88.jpg");//头像
    }

    @PostMapping("/logout")
    public Result userLogOut() {
        log.warn("退出啦啦啦啦啦");
        return Result.ok();
    }


    @ApiOperation("根据用户编号获取用户信息")
    @PostMapping("/findUserById/{id}")
    public Result findUserById(@ApiParam(value = "用户编号", required = true) @PathVariable("id") String userId) {
        User user = iUserService.getById(userId);
        return Result.ok().data("user", user);
    }


    @ApiOperation("根据用户编号删除用户信息")
    @GetMapping("/removeUser/{id}")
    public Result removeUser(@ApiParam(value = "用户编号", required = true) @PathVariable("id") String userId) {
        boolean b = iUserService.removeById(userId);
        return b?Result.ok():Result.error();
    }

    @ApiOperation(value = "新增用户")
    @PostMapping("saveUser")
    public Result saveUser(@ApiParam(value = "新增的用户具体信息") @RequestBody User User){
        boolean save = iUserService.save(User);
        return Result.ok().success(save).msg(save?"成功":"失败");
    }

    @ApiOperation(value = "修改用户")
    @PostMapping("updateUser")
    public Result updateUser(@ApiParam(value = "修改的用户具体信息") @RequestBody User User) {
        boolean update = iUserService.updateById(User);
        return update ? Result.ok() : Result.error();
    }


    @ApiOperation("根据用户姓名获取用户信息")
    @GetMapping("findByName/{name}")
    public Result findUserByName(@PathVariable("name") String name) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)) {
            userQueryWrapper.eq("user_name", name);
            User one = iUserService.getOne(userQueryWrapper);
            log.warn(one.toString());
            return Result.ok().data("user", one);
        } else {
            return Result.error().msg("未输入姓名");
        }
    }

    @ApiOperation(value = "分页显示用户信息")
    @GetMapping("/listUserByPage/{pageNum}/{pageSize}")
    public Result listUserByPage(@ApiParam(value = "当前页码数", required = true) @PathVariable("pageNum") int pageNum,
                                 @ApiParam(value = "每页条数", required = true) @PathVariable("pageSize") int pageSize) {

        Page<User> pageUser = new Page<>(pageNum, pageSize);
        iUserService.page(pageUser);

        long total = pageUser.getTotal(); //-- 总记录数
        List<User> records = pageUser.getRecords();  //-- 数据集合

        return Result.ok().data("total", total).data("list", records);
    }

    @ApiOperation("根据条件分页显示用户信息")
    @PostMapping("/listUserByPageAndCondition/{pageNum}/{pageSize}")
    public Result listTeacherByPageAndCondition(@ApiParam(name = "pageNum", value = "当前页码数", required = true) @PathVariable("pageNum") int pageNum,
                                                @ApiParam(name = "pageSize", value = "每页条数", required = true) @PathVariable("pageSize") int pageSize,
                                                @ApiParam(name = "userQuery", value = "用户查询条件", required = false) @RequestBody UserQuery userQuery) {

        Page<User> pageTeacher = new Page<>(pageNum, pageSize);

        //-- 构建查询条件类。MyBatis Plus的类，
        QueryWrapper<User> wrapper = new QueryWrapper<>();

        //-- 获取具体的查询信息
        String name = userQuery.getName();
        String gender = userQuery.getGender();
        Date birthday = userQuery.getBirthday();
        String area = userQuery.getArea();
        String phone = userQuery.getPhone();


        //-- 根据实际的参数情况完成QueryWrapper的赋值
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("user_name", name);
        }

        if (!StringUtils.isEmpty(gender)) {
            wrapper.eq("gender", gender);
        }

        if (!StringUtils.isEmpty(phone)) {
            wrapper.like("user_phone", phone);
        }

        if (!StringUtils.isEmpty(area)) {
            wrapper.like("user_area", area);
        }

        wrapper.orderByDesc("gmt_modified");

        //-- 进行分页查询的方法
        iUserService.page(pageTeacher, wrapper);

        long total = pageTeacher.getTotal(); //-- 总记录数
        List<User> records = pageTeacher.getRecords();  //-- 数据集合

        return Result.ok().data("total", total).data("list", records);
    }





}


